import {
  GetAllConfig,
  ResetAllConfig,
  SetConfig,
  IncConfig,
  DecConfig,
  ResetConfig,
} from '@/wailsjs/go/app/app'
import { Conf } from '@/types/go'

const lineDesign = reactive({
  'font-size': {
    value: 16,
    label: '字体',
    fixed: 0,
    unit: 'px',
    name: 'FontSize',
  },
  'line-height': {
    value: 1.5,
    label: '行高',
    fixed: 1,
    unit: 'em',
    name: 'LineHeight',
  },
  'letter-spacing': {
    value: 0,
    label: '间距',
    fixed: 1,
    unit: 'px',
    name: 'LetterSpacing',
  },
  'font-weight': {
    value: 400,
    label: '粗细',
    fixed: 0,
    unit: '',
    name: 'FontWeight',
  },
})

const colorClass = reactive({
  bg: {
    label: '背景',
    value: '#ffffff',
    show: true,
    name: 'BackgroundColor',
  },
  font: {
    label: '字体',
    value: '#000000',
    show: true,
    name: 'FontColor',
  },
  title: {
    label: '标题',
    value: '#665f5f',
    show: true,
    name: 'TitleColor',
  },
  progress: {
    label: '进度',
    value: '#000000',
    show: true,
    name: 'ProgressColor',
  },
})

async function initStyle() {
  const config = await GetAllConfig()
  for (const key in lineDesign) {
    const k1 = key as keyof typeof lineDesign
    lineDesign[k1].value = Number(config[lineDesign[k1].name as keyof Conf])
  }
  for (const key in colorClass) {
    const k1 = key as keyof typeof colorClass
    colorClass[k1].value = config[colorClass[k1].name as keyof Conf] + ''
  }
}

async function resetStyle() {
  const config = await ResetAllConfig()
  for (const key in lineDesign) {
    const k1 = key as keyof typeof lineDesign
    lineDesign[k1].value = Number(config[lineDesign[k1].name as keyof Conf])
  }
  for (const key in colorClass) {
    const k1 = key as keyof typeof colorClass
    colorClass[k1].value = config[colorClass[k1].name as keyof Conf] + ''
  }
}

async function changeLineDesign(
  // key: keyof typeof lineDesign,
  key: string,
  action: 'up' | 'down' | 'reset',
) {
  const actionIPC = {
    up: IncConfig,
    down: DecConfig,
    reset: ResetConfig,
  }
  const k1 = key as keyof typeof lineDesign
  const value = await actionIPC[action](lineDesign[k1].name)
  lineDesign[k1].value = Number(value)
}

async function changeColorClass(key: keyof typeof colorClass) {
  await SetConfig(colorClass[key].name, colorClass[key].value)
}

async function setTheme(bg: string, fg: string) {
  await SetConfig('BackgroundColor', bg)
  colorClass['bg'].value = bg
  await SetConfig('FontColor', fg)
  colorClass['font'].value = fg
}

export {
  initStyle,
  colorClass,
  changeColorClass,
  changeLineDesign,
  lineDesign,
  resetStyle,
  setTheme,
}
